using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace moDb_prototype
{
    public class Reflector
    {
        /// <summary>
        /// Reflects the properties.
        /// </summary>
        /// <param name="theObject">The object.</param>
        /// <returns></returns>
        public static PropertyInfo[] ReflectedProperties(object theObject)
        {
            return Reflector.ReflectedProperties(theObject.GetType());
        }

        /// <summary>
        /// Reflects the properties.
        /// </summary>
        /// <param name="theType">The type.</param>
        /// <returns></returns>
        public static PropertyInfo[] ReflectedProperties(Type theType)
        {
            List<PropertyInfo> returnValue = new List<PropertyInfo>(); 

            foreach (PropertyInfo prop in 
                theType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (propertyFilter(prop))
                {
                    returnValue.Add(prop);
                }
            }
            return returnValue.ToArray();          
        }

        /// <summary>
        /// Gets the reflected property.
        /// </summary>
        /// <param name="theObject">The object.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static PropertyInfo GetReflectedProperty(object theObject, string name)
        {
            PropertyInfo[] properties = ReflectedProperties(theObject);
            foreach (PropertyInfo property in properties)
            {
                if (property.Name == name)
                    return property;                
            }
            //if the property couldn't be found throw a KeyNotFoundException
            throw new KeyNotFoundException(String.Format(
                "Couldn't find property '{0}' in the type {1}",name, 
                theObject.GetType().ToString()));   
        }
        private static bool propertyFilter(PropertyInfo property)
        {
            return property.Name != "IsNull"; 
        }
    }
}
